<template>
	<view>
		<view class="mine-top"></view>
		<view class="mine-head">		
			<image :src=avatar_url mode="aspectFit" alt=''></image>
			<view>
				<view>{{nick_name}}</view>
				<text>{{phone}}</text>
				<br/>
				<text>{{invitationCode}}</text>				
			</view>
			<view :class="{'btn-visible': afterAuth}">
				<button @getuserinfo="getUserInfo" open-type="getUserInfo" class="login-btn" >点击登录</button>			
			</view>
			<view @click="turnPage('authentication/index')" :class="{'visible': authStatus}">
				<text>{{goToAuth}}</text>
				<image src="../../static/icon_enter.png" mode="aspectFit"></image>
			</view>		
		</view>
		<view class="mine-list">
			<view @click="_turnPage('agent/index')"  v-if="notSignedAgent">
				<image src="../../static/m_qyjjr.png" mode="aspectFit"></image>
				<view>签约经纪人</view>
				<view>
					{{signedAgentName}}
					<image src="../../static/icon_enter.png" mode="aspectFit"></image>					
				</view>
			</view>
			<view @click="_turnPage('newAgent/index')">
				<image src="../../static/m_xjjr.png" mode="aspectFit"></image>
				<view>新经纪人</view>
				<view>
					<text>{{newAgentNum}}</text>
					<image src="../../static/icon_enter.png" mode="aspectFit"></image>
				</view>
			</view>
			<view @click="_turnPage('apply/index')">
				<image src="../../static/m_qysq.png" mode="aspectFit"></image>
				<view>签约申请</view>
				<view>
					<text>{{submitApplicationNum}}</text>
					<image src="../../static/icon_enter.png" mode="aspectFit"></image>
				</view>
			</view>
			<view>
				<image src="../../static/m_xttz.png" mode="aspectFit"></image>
				<view>系统通知</view>
				<view>
					<text>0</text>
					<image src="../../static/icon_enter.png" mode="aspectFit"></image>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
import store from "@/store/store.vue"
export default{
	onLoad(){		
	},
	onShow(){
		if(getApp().globalData.avatar_url == ''){
			this.phone = ''
		}
		
		if(getApp().globalData.avatar_url == '' || getApp().globalData.nick_name == ''){
			this.afterAuth = false
			this.authStatus = true
		}
		if(getApp().globalData.avatar_url != '' && getApp().globalData.nick_name != ''){
			this.afterAuth = true
			this.getAuthStatus()
			// TODO 多次网络请求，合并在一起，然后请求一次，缓存下来
			this.getNewAgentNum()
			this.getSignedAgentName()
			this.getSubmitApplicationNum()
		}				
	},
	data(){
		return {			
			isLogin: false,
			invitationCode:'',
			waitAuth: false,
			goToAuth: '去认证',
			submitApplicationNum:0,
			newAgentNum: 0,
			notSignedAgent: true,
			signedAgentName: '',
			afterAuth: false,
			authStatus: false,
			phone: getApp().globalData.phone,
			avatar_url: getApp().globalData.avatar_url == '' ? '../../static/avatar.png' : getApp().globalData.avatar_url,
			nick_name: getApp().globalData.nick_name,			
		}
	},
	methods:{
		getAuthStatus(){
			let _this = this			
			uni.request({
			    url: store.url + '/user/check_auth_status?openId=' + getApp().globalData.open_id,
				method: 'GET',
			    header: {
					"content-type": "application/json;charset=utf-8"						
			    },
			    success: (res) => {		
					if(res.data.status == 'success'){
						if(res.data.message == 'ok'){
							_this.authStatus = true
							_this.invitationCode = '邀请码: ' + res.data.invitation_code
							_this.phone = res.data.phone
							getApp().globalData.user_id = res.data.user_id
							getApp().globalData.user_name = res.data.user_name
							_this.waitAuth = false
						}
						if(res.data.message == 'no'){
							_this.goToAuth = '认证中'
							_this.waitAuth = true
							_this.invitationCode = '邀请码: ' + res.data.invitation_code
							_this.phone = res.data.phone		
						}						
					}else{													
						_this.authStatus = false
						_this.goToAuth = '去认证'
						_this.waitAuth = false
						_this.invitationCode = '邀请码: '
						_this.submitApplicationNum = 0
						_this.newAgentNum = 0
						_this.notSignedAgent = true
						_this.signedAgentName = ''
						// TODO 微信手机号解密
						_this.phone = ''
						getApp().globalData.user_id = 0
						getApp().globalData.inviter = 0
						getApp().globalData.user_name = ''
						getApp().globalData.inviter_name = 0
					}															      
			    },
				fail: (res)=>{
					uni.showModal({
					    title: '提示',
					    content: '访问服务器失败: ' + res.data.message
					});		
					getApp().globalData.user_id = 0
				}
			});	
		},
		getSignedAgentName(){
			let _this = this
			uni.request({
				url: store.url + '/user/get_signed_agent_name?openId=' + getApp().globalData.open_id,
				method: 'GET',
				header: {
					"content-type": "application/json;charset=utf-8"						
				},
				success: (res) => {		
					console.log(res)
					if(res.data.status == 'success'){
						_this.signedAgentName = res.data.message.user_name		
						getApp().globalData.inviter_id = res.data.message.id
						getApp().globalData.inviter_name = res.data.message.user_name		
						_this.notSignedAgent = true												
					}else if(res.data.status == 'not_agent'){
						_this.notSignedAgent = false
						getApp().globalData.inviter_id = 0
						getApp().globalData.inviter_name = ''
					}else{						
						_this.notSignedAgent = true		
						getApp().globalData.inviter_id = 0
						getApp().globalData.inviter_name = ''
					}															      
				},
				fail: (res)=>{
					uni.showModal({
						title: '提示',
						content: '访问服务器失败: ' + res.data.message
					});						
				}
			});	
		},
		getNewAgentNum(){
			let _this = this
			uni.request({
				url: store.url + '/user/get_new_agent_num?openId=' + getApp().globalData.open_id,
				method: 'GET',
				header: {
					"content-type": "application/json;charset=utf-8"						
				},
				success: (res) => {		
					console.log(res)
					if(res.data.status == 'success'){
						_this.newAgentNum = parseInt(res.data.message)						
					}else{
						_this.newAgentNum = 0	
					}															      
				},
				fail: (res)=>{
					uni.showModal({
						title: '提示',
						content: '访问服务器失败: ' + res.data.message
					});						
				}
			});	
		},
		getSubmitApplicationNum(){
			let _this = this
			uni.request({
				url: store.url + '/user/get_submit_application_num?openId=' + getApp().globalData.open_id,
				method: 'GET',
				header: {
					"content-type": "application/json;charset=utf-8"						
				},
				success: (res) => {		console.log(res)
					if(res.data.status == 'success'){
						_this.submitApplicationNum = res.data.message						
					}else{
						_this.submitApplicationNum = 0
					}															      
				},
				fail: (res)=>{
					uni.showModal({
						title: '提示',
						content: '访问服务器失败: ' + res.data.message
					});						
				}
			});	
		},
		getUserInfo(e) {		
			console.log(getApp().globalData.open_id)
			getApp().globalData.avatar_url = e.detail.userInfo.avatarUrl
			getApp().globalData.nick_name = e.detail.userInfo.nickName
			this.avatar_url = e.detail.userInfo.avatarUrl
			this.nick_name = e.detail.userInfo.nickName
			this.phone = getApp().globalData.phone
			this.afterAuth = true
			this.authStatus = false
			this.getAuthStatus()
			this.getSignedAgentName()
			this.getNewAgentNum()
			this.getSubmitApplicationNum()		
			this.isLogin = true
			getApp().globalData.isLogin = true
		},
		_turnPage(url){	
			if(this.waitAuth == true){
				uni.showModal({
					title: '提示',
					content: '请等待邀请人审核'
				});	
				return
			}else if(this.isLogin == false){
				uni.showModal({
					title: '提示',
					content: '请先登录'
				});	
				return
			}else if(this.authStatus == false){
				uni.showModal({
					title: '提示',
					content: '请先去认证'
				});	
			}else{
				uni.navigateTo({
					url: '/pages/' + url
				})
			}			
		}
	}
}
	
</script>

<style lang="scss" scoped>
.login-btn{
	position: absolute;
	top: 100rpx;
	left: 205rpx;
	font-size:36rpx;
	background: none;
	border: 0!important;
	outline: 0;
	font-size: 18px;
	width: 100px;
	color: #00aaee;
}
.hidden{
	visibility: hidden;
}
.mine_visible{
	visibility: visible!important;
}
.btn-visible>.login-btn{
	display: none;
}
.visible{
	visibility: hidden;
}
.mine-top {
	padding-top: 50rpx;
	box-sizing: border-box;
	height: 160rpx;
	background-image: linear-gradient(180deg, #f6613e, #f13b32);
}

.mine-head {
	display: flex;
	margin: -110rpx auto 0;
	padding: 30rpx 20rpx;
	box-sizing: border-box;
	border-radius: 10rpx;
	align-items: center;
	justify-content: space-between;
	width: 90vw;
	background: #fff;

	image {
		width: 136rpx;
		height: 136rpx;
		border-radius: 0;
		overflow: hidden;
	}

	> view {
		font-size: 24rpx;

		&:first-of-type {
			padding: 0 20rpx;
			box-sizing: border-box;
			flex: 1;

			view {
				line-height: 1.5;
				font-size: 34rpx;
				font-weight: bold;
			}

			text {
				color: #bdbdbd;
			}
		}

		&:last-of-type {
			display: flex;

			text {
				line-height: 88rpx;
				color: #00a0e9;
			}

			image {
				margin-left: 20rpx;
				width: 18rpx;
				height: 88rpx;
			}
		}
	}
}

.mine-list {
	padding: 0 5vw;
	
	>view {
		display: flex;
		margin-top: 16rpx;
		padding: 36rpx 20rpx 36rpx 50rpx;
		box-sizing: border-box;
		justify-content: space-between;
		align-items: center;
		background: #FFF;
		
		image {
			width: 32rpx;
			height: 32rpx;
		}
		
		view {
			&:first-of-type {
				margin-left: 20rpx;
				flex: 1;
			}
			
			&:last-of-type {
				display: flex;
				align-items: center;
				font-size: 24rpx;
				color: #bdbdbd;
				
				image {
					margin-left: 20rpx;
					width: 18rpx;
					height: 24rpx;
				}
			}
			
			text {
				display: block;
				padding: 0 10rpx;
				box-sizing: border-box;
				border-radius: 50%;
				line-height: 38rpx;
				min-width: 38rpx;
				text-align: center;
				font-size: 24rpx;
				color: #FFF;
				background: #f13b32;
			}
		}
	}
}
</style>
